<?php
namespace app\index\controller;

use app\common\controller\Common;
use org\Page;
use think\facade\Config;

class Index extends Common
{
    protected function initialize()
    {
        parent::initialize(); //
    }

    public function index()
    {

        $data = curl_server(sprintf("apiv2/classifylist?cid=%s&page=%d&page_size=%d&flag_type=home_cid_list",0,1,24));
        $hot_tags_list = curl_server(sprintf("apiv2/speciallist?hot=true&page=%d&page_size=%d",1,32));
        $hot_tags_list = $hot_tags_list['data']['list'];
        $data = $data['data'];
        $home_cid_list = $data['list'];

        //热门资讯
        $hot_news = curl_server(sprintf("apiv2/newslist?cid=%d&page=%d&page_size=%d&hot=true",0,1,12));
        $hot_new_list = $hot_news['data']['list'];

        $this->site_seo('home');
        return $this->fetch("index",['home_cid_list' => $home_cid_list,'hot_tags_list' => $hot_tags_list,'hot_new_list' => $hot_new_list]);
    }

    public function search ($k = '')
    {
        if (empty($k)) {
            $this->error("搜索关键字不能为空！");
        }

        $searchList = curl_server("apiv2/search?keyword={$k}");
        $this->site_seo('search',['keyword' => $k]);

        return $this->fetch("search",['keyword' => $k,'list' => $searchList['data']]);
    }

    /**
     * 清理缓存
     */
    public function clean_cache () {
        $appkey = $this->request->post("appkey","");
        if (empty($appkey)) {
            $config = Config::pull("server");
            if (trim($config['app_key']) == trim($appkey)) {

            }
        }
    }

    public function top ($classify = '') {

        $this->assign("current_classify",$classify);

        if (empty($classify))
        {
            $this->site_seo('top');
            return $this->fetch("top");
        }else {
            $this->site_seo('top');
            return $this->fetch("top_classify");
        }
    }
}
